// JavaScript Document
//判断用户名是否合法
function Uname()
{
  var returnvalue = true;
  var value = new Array();
  value[0] = document.getElementById('name').value;
  value[1] = document.getElementById('testname');
  if (value[0] == '')
  {
    value[1].className = 'false';
    value[1].innerHTML = '请填写用户名';
    return returnvalue = false;
  };
  $.ajax({
    type: 'POST',
    url: 'DispatchAction.action?method=judge',
    data: 'name=' + value[0],
    async: true,
    success: function (msg) {
      if (msg == 'true')
      {
        value[1].className = 'true1';
        value[1].innerHTML = '√';
        return returnvalue = true;
      } 
      else {
        value[1].className = 'false';
        value[1].innerHTML = '用户名已存在';
        return returnvalue = false;
      }
    }
  });
  return returnvalue;
}//判断密码是否为空处理

function Pword()
{
  var value = document.getElementById('password').value;
  var id = document.getElementById('testpassword');
  if (value == '')
  {
    id.className = 'false';
    id.innerHTML = '请填写密码';
    return false;
  }
  if (value != '') {
    id.className = 'true1';
    id.innerHTML = '√';
    return true;
  }
}//判断密码正确否和是否为空

function Pword1()
{
  var value = document.getElementById('password').value;
  var value0 = document.getElementById('okpassword').value;
  var id = document.getElementById('testokpassword');
  if (value == value0 && Pword())
  {
    id.className = 'true1';
    id.innerHTML = '√';
    return true;
  }
  else
  {
	    id.className = 'false';
	    id.innerHTML = '密码不对!';
	    return false;
	  }
}//答案一是否为空

function AA()
{
  var value = document.getElementById('AnswerA').value;
  var id = document.getElementById('testAnswerA');
  if (value == '')
  {
    id.className = 'false';
    id.innerHTML = '请输入答案';
    return false;
  }
  if (value != '')
  {
    id.className = 'true1';
    id.innerHTML = '√';
    return true;
  }
}//答案二是否为空

function AB()
{
  var value = document.getElementById('AnswerB').value;
  var id = document.getElementById('testAnswerB');
  if (value == '')
  {
    id.className = 'false';
    id.innerHTML = '请输入答案';
    return false;
  }
  if (value != '')
  {
    id.className = 'true1';
    id.innerHTML = '√';
    return true;
  }
}//邮箱格式是否正确

function email()
{
  var value = new Array();
  value[0] = document.getElementById('e-mail').value;
  value[1] = document.getElementById('teste-mail');
  var reg = new RegExp('^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$');
  if (!reg.test(value[0] || value[0] == null))
  {
    value[1].className = 'false';
    value[1].innerHTML = '邮箱格式不对';
    document.getElementById('e-mail').value = '';
    return false;
  } 
  else {
    value[1].className = 'true1';
    value[1].innerHTML = '√';
    return true;
  }  //window.history.back(-1);   

}//验证码判空

var Vrity = function ()
{ 
  var value = document.getElementById('verity').value;
  var id = document.getElementById('testverity');
  if (value == '')
  {
    id.className = 'false';
    id.innerHTML = '验证码为空';
    return false;
  }
  if (value != '')
  {
    id.className = 'true1';
    id.innerHTML = '';
    return true;
  }
};//刷新验证码

function image()
{
  //	 document.getElementsByTagName('img')[0].setAttribute('src','');
  //		alert(1);
  document.getElementsByTagName('img') [1].setAttribute('src', 'DispatchAction.action?method=getImage&id=' + Math.random() * 10);
}//注册用户含有头像

$(function () {
  $('#file_upload').change(function () {
    var filepath = $('#file_upload').val();
    var extStart = filepath.lastIndexOf('.');
    var ext = filepath.substring(extStart, filepath.length).toUpperCase();
    if (ext != '.BMP' && ext != '.PNG' && ext != '.GIF' && ext != '.JPG' && ext != '.JPEG')
    {
      alert('图片限于bmp,png,gif,jpeg,jpg格式');
      return false;
    }
    var fileObj = $('#file_upload') [0];
    var file = fileObj.files[0];
    if (file['size'] <= 0)
    {
      alert('请选择文件!');
      return false;
    }
    if ((file['size'] / 1024) > 2048)
    {
      alert('图片不能大于2M');
      return false;
    }
    var windowURL = window.URL || window.webkitURL;
    var dataURL;
    var $img = $('#preview');
    if (fileObj && fileObj.files && fileObj.files[0]) {
      dataURL = windowURL.createObjectURL(file);
      $img.attr('src', dataURL);
    } 
    else {
      dataURL = $file.val();
      var imgObj = document.getElementById('preview');
      // 两坑:
      // 1、设置filter属性元素必须已经存DOM树态创建Node需要设置属性前加入DOM先设置属性加入效；
      // 2、src属性需要像面式添加面两种式添加效；
      imgObj.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)';
      imgObj.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = dataURL;
    }
  });
});

function falsenull()
{
  var uname = Uname();
  var pword = Pword();
 var pword1 = Pword1();
  var aa = AA();
  var ab = AB();
  var Email = email();
  var vrity = Vrity();
  var img_value= $('#file_upload').val();
  if(img_value==""){
	  alert("请上传头像");
	  return;}
   if (uname && pword && pword1 && aa && ab && Email && vrity )
   {
    var verify = $('#verity').val();
    var formData = new FormData(document.getElementById('form'));
    $.ajax({
      type: 'POST',
      url: 'DispatchAction.action?method=verify',
      data: {
        "verify": verify
      },
      success: function (msg) {
        if (msg == 'verityfalse') alert('验证码输入错误!');
         else {
          $.ajax({
            url: 'DispatchAction.action?method=insertPenson',
            type: 'POST',
            data: formData,
            // 告诉jQuery不要去处理发送的数据
            processData: false,
            // 告诉jQuery不要去设置Content-Type请求头
            contentType: false,
            success: function (response) {
              if (response == 'true')
              {
                alert('注册成功,请登录');
                window.location.href="important.jsp";
              } 
              else
              alert('注册失败');
            }
          });
        }
      }
    });
  }
}//将传入的头像，在img中显示
